'''
Created on 18/09/2009

@author: Edward Huynh
'''
from Queue import Queue
from datastore.resource import Resource
from datastore.uri import Uri
from runtime.spaces import Spaces, Node, UriTree, SpacesContext
import logging
import time
import thread
import threading
import unittest

class SpacesContextTest(unittest.TestCase):
    '''
    MOST of these tests test ONLY delegation to the Spaces class, as most of the methods
    in SpacesContext do just delegation.
    '''
    
    def test_get_case_1(self):
        '''[SpacesContext] get with no callback'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_spaces_queue = Queue()
        dummy_spaces_context = SpacesContext(spaces, dummy_spaces_queue)        
        node_1 = Node('dummyuri')
        node_1.resource = dummy_resource
        spaces.uri_tree.add_node(node_1, uri)        
        self.assertEqual(dummy_spaces_context.get(uri.dummyuri), dummy_resource)

    def test_get_case_2(self):
        '''[SpacesContext] get with callback'''
        dummy_base = 'http://dummy.com'
        dummy_method_name = 'dummy_method_name'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_spaces_queue = Queue()
        dummy_spaces_context = SpacesContext(spaces, dummy_spaces_queue)
        node_1 = Node('dummyuri')
        node_1.resource = dummy_resource
        sample_kwargs = {'one':1, 'two':2}
        spaces.uri_tree.add_node(node_1, uri)
        self.assertEqual(dummy_spaces_context.get(uri.dummyuri, dummy_method_name, **sample_kwargs), None)
        self.assertEqual(dummy_spaces_context.queue.get(True), (dummy_method_name, 'get', uri.dummyuri, dummy_resource, None, dummy_spaces_context, sample_kwargs))

    def test_put_case_1(self):
        '''[SpacesContext] put with no callback'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_spaces_queue = Queue()
        dummy_spaces_context = SpacesContext(spaces, dummy_spaces_queue)
        self.assertEqual(dummy_spaces_context.put(uri.dummyuri, dummy_resource), uri.dummyuri)
    
    def test_put_case_2(self):
        '''[SpacesContext] put with callback'''
        dummy_base = 'http://dummy.com'
        dummy_method_name = 'dummy_method_name'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_spaces_queue = Queue()
        dummy_spaces_context = SpacesContext(spaces, dummy_spaces_queue)
        sample_kwargs = {'one':1, 'two':2}
        self.assertEqual(dummy_spaces_context.put(uri.dummyuri, dummy_resource, dummy_method_name, **sample_kwargs), None)
        self.assertEqual(dummy_spaces_queue.get(True), (dummy_method_name, 'put', uri.dummyuri, uri.dummyuri, None, dummy_spaces_context, sample_kwargs))
        
    def test_find_case_1(self):
        '''[SpacesContext] find with no callback'''
        dummy_base_1 = 'http://dummy1.com'
        uri_1 = Uri(dummy_base_1).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_spaces_queue = Queue()
        dummy_spaces_context = SpacesContext(spaces, dummy_spaces_queue)
        dummy_spaces_context.get(uri_1.a.b.c.d)
        dummy_spaces_context.get(uri_1.a.b.e)
        dummy_spaces_context.get(uri_1.a.b.f.g)
        uris = dummy_spaces_context.find(uri_1.a.b.collection(True))
        self.assertEqual(len(uris), 3)
        self.assertTrue(uri_1.a.b.c.collection(True) in uris)
        self.assertTrue(uri_1.a.b.e in uris)
        self.assertTrue(uri_1.a.b.f.collection(True) in uris)  
    
    def test_find_case_2(self):
        '''[SpacesContext] find with callback'''
        dummy_base_1 = 'http://dummy1.com'
        dummy_method_name = 'dummy_method_name'
        uri = Uri(dummy_base_1).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_spaces_queue = Queue()
        dummy_spaces_context = SpacesContext(spaces, dummy_spaces_queue)
        dummy_spaces_context.get(uri.a.b.c.d)
        dummy_spaces_context.get(uri.a.b.e)
        dummy_spaces_context.get(uri.a.b.f.g)
        sample_kwargs = {'one':1, 'two':2}
        self.assertEqual(dummy_spaces_context.find(uri.a.b.collection(True), dummy_method_name, **sample_kwargs), None)
        (callback_method_name, method, uri.dummyuri, uris, result, caller_spaces_context, kwargs) = dummy_spaces_queue.get(True)
        self.assertEqual(callback_method_name, dummy_method_name)
        self.assertEqual(method, 'find')
        self.assertEqual(result, None)
        self.assertEqual(len(uris), 3)
        self.assertTrue(uri.a.b.c.collection(True) in uris)
        self.assertTrue(uri.a.b.e in uris)
        self.assertTrue(uri.a.b.f.collection(True) in uris)
        self.assertEqual(caller_spaces_context, dummy_spaces_context)
        self.assertEqual(kwargs, sample_kwargs)  
    
    def test_delete_case_1(self):
        '''[SpacesContext] delete with no callback'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_spaces_queue = Queue()
        dummy_spaces_context = SpacesContext(spaces, dummy_spaces_queue)
        node = Node('dummyuri')
        node.resource = dummy_resource
        spaces.uri_tree.add_node(node, uri)     
        self.assertEqual(dummy_spaces_context.delete(uri.dummyuri), None)
        self.assertNotEqual(spaces.uri_tree.find_node(uri.dummyuri), dummy_resource)

    def test_delete_case_2(self):
        '''[SpacesContext] delete with callback'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()
        dummy_method_name = 'dummy_method_name'
        spaces = Spaces()
        dummy_spaces_queue = Queue()
        dummy_spaces_context = SpacesContext(spaces, dummy_spaces_queue)
        node = Node('dummyuri')
        node.resource = dummy_resource
        spaces.uri_tree.add_node(node, uri)
        sample_kwargs = {'one':1, 'two':2}
        self.assertEqual(dummy_spaces_context.delete(uri.dummyuri, dummy_method_name, **sample_kwargs), None)
        self.assertNotEqual(spaces.uri_tree.find_node(uri.dummyuri), dummy_resource)
        (callback_method_name, method, uri.dummyuri, return_value, result, caller_spaces_queue, kwargs) = dummy_spaces_queue.get(True)
        self.assertEqual(callback_method_name, dummy_method_name)
        self.assertEqual(method, 'delete')
        self.assertEqual(result, None)  
        self.assertEqual(return_value, None)
        self.assertEqual(caller_spaces_queue, dummy_spaces_context)
        self.assertEqual(kwargs, sample_kwargs)
        
    def test_remove_case_1(self):
        '''[SpacesContext] remove with no callback'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_spaces_queue = Queue()
        dummy_spaces_context = SpacesContext(spaces, dummy_spaces_queue)   
        node = Node('dummyuri')
        node.resource = dummy_resource
        spaces.uri_tree.add_node(node, uri) 
        self.assertEqual(dummy_spaces_context.remove(uri.dummyuri), dummy_resource)
        self.assertNotEqual(spaces.uri_tree.find_node(uri.dummyuri), dummy_resource)

    def test_remove_case_2(self):
        '''[SpacesContext] remove with callback'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()
        dummy_method_name = 'dummy_method_name'
        spaces = Spaces()
        dummy_spaces_queue = Queue()
        dummy_spaces_context = SpacesContext(spaces, dummy_spaces_queue)   
        node = Node('dummyuri')
        node.resource = dummy_resource
        spaces.uri_tree.add_node(node, uri)
        sample_kwargs = {'one':1, 'two':2}
        self.assertEqual(dummy_spaces_context.remove(uri.dummyuri, dummy_method_name, **sample_kwargs), None)
        self.assertNotEqual(spaces.uri_tree.find_node(uri.dummyuri), dummy_resource)
        (callback_method_name, method, uri.dummyuri, return_value, result, caller_spaces_context, kwargs) = dummy_spaces_context.queue.get(True)
        self.assertEqual(callback_method_name, dummy_method_name)
        self.assertEqual(method, 'remove')
        self.assertEqual(result, None)  
        self.assertEqual(return_value, dummy_resource)  
        self.assertEqual(caller_spaces_context, dummy_spaces_context)
        self.assertEqual(kwargs, sample_kwargs)
    
    def test_watch(self):
        '''[SpacesContext] watch'''
        dummy_base = 'http://dummy.com'
        dummy_method_name = 'dummy_method_name'
        uri = Uri(dummy_base).collection(True)
        spaces = Spaces()
        dummy_spaces_queue = Queue()
        dummy_spaces_context = SpacesContext(spaces, dummy_spaces_queue)  
        sample_kwargs = {'one':1, 'two':2}
        self.assertNotEqual(dummy_spaces_context.watch(['get'], uri, dummy_method_name, **sample_kwargs), None)
        self.assertEqual(spaces.uri_tree.find_node(uri).watch['get'], (dummy_method_name, dummy_spaces_context, sample_kwargs))
    
    def test_watch_all(self):
        '''[SpacesContext] watch_all'''
        dummy_base = 'http://dummy.com'
        dummy_method_name = 'dummy_method_name'
        uri = Uri(dummy_base).collection(True)
        spaces = Spaces()
        dummy_spaces_queue = Queue()
        dummy_spaces_context = SpacesContext(spaces, dummy_spaces_queue)
        sample_kwargs = {'one':1, 'two':2}  
        self.assertNotEqual(dummy_spaces_context.watch_all(['get'], uri, dummy_method_name, **sample_kwargs), None)
        self.assertEqual(spaces.uri_tree.find_node(uri).watch_all['get'], (dummy_method_name, dummy_spaces_context, sample_kwargs))
    
    def test_delete_watch(self):
        '''[SpacesContext] delete watch'''
        dummy_base = 'http://dummy.com'
        dummy_method_name = 'dummy_method_name'
        uri = Uri(dummy_base).collection(True)
        spaces = Spaces()
        dummy_spaces_queue = Queue()
        dummy_spaces_context = SpacesContext(spaces, dummy_spaces_queue)  
        watch_ref = dummy_spaces_context.watch(['get'], uri, dummy_method_name)
        self.assertEqual(spaces.uri_tree.find_node(uri).watch['get'], (dummy_method_name, dummy_spaces_context, {}))
        self.assertEqual(dummy_spaces_context.delete_watch(watch_ref), None)
        self.assertFalse('get' in spaces.uri_tree.find_node(uri).watch)
        
    def test_process(self):
        '''[SpacesContext] process'''
        dummy_base = 'http://dummy.com'
        dummy_method_name_get = 'dummy_method_name_get'
        dummy_method_name_put = 'dummy_method_name_put'
        dummy_method_name_delete = 'dummy_method_name_delete'
        dummy_method_name_find = 'dummy_method_name_find'
        dummy_method_name_remove = 'dummy_method_name_remove'
        uri = Uri(dummy_base).collection(True)
        spaces = Spaces()
        dummy_spaces_queue = Queue()
        dummy_spaces_context = SpacesContext(spaces, dummy_spaces_queue)  
        sample_kwargs = {'one':1, 'two':2}  
        self.assertNotEqual(dummy_spaces_context.process(uri, 
            get=dummy_method_name_get, 
            put=dummy_method_name_put, 
            delete=dummy_method_name_delete, 
            find=dummy_method_name_find, 
            remove=dummy_method_name_remove,
            **sample_kwargs
            ), None)
        self.assertEqual(spaces.uri_tree.find_node(uri).process['get'], (dummy_method_name_get, dummy_spaces_context, sample_kwargs))
        self.assertEqual(spaces.uri_tree.find_node(uri).process['put'], (dummy_method_name_put, dummy_spaces_context, sample_kwargs))
        self.assertEqual(spaces.uri_tree.find_node(uri).process['delete'], (dummy_method_name_delete, dummy_spaces_context, sample_kwargs))
        self.assertEqual(spaces.uri_tree.find_node(uri).process['find'], (dummy_method_name_find, dummy_spaces_context, sample_kwargs))  
        self.assertEqual(spaces.uri_tree.find_node(uri).process['remove'], (dummy_method_name_remove, dummy_spaces_context, sample_kwargs))  
        
    def test_process_all(self):
        '''[SpacesContext] process_all'''
        dummy_base = 'http://dummy.com'
        dummy_method_name_get = 'dummy_method_name_get'
        dummy_method_name_put = 'dummy_method_name_put'
        dummy_method_name_delete = 'dummy_method_name_delete'
        dummy_method_name_find = 'dummy_method_name_find'
        dummy_method_name_remove = 'dummy_method_name_remove'
        uri = Uri(dummy_base).collection(True)
        spaces = Spaces()
        dummy_spaces_queue = Queue()
        dummy_spaces_context = SpacesContext(spaces, dummy_spaces_queue)
        sample_kwargs = {'one':1, 'two':2}  
        self.assertNotEqual(dummy_spaces_context.process_all(uri,  
            get=dummy_method_name_get, 
            put=dummy_method_name_put, 
            delete=dummy_method_name_delete, 
            find=dummy_method_name_find, 
            remove=dummy_method_name_remove,
            **sample_kwargs
            ), None)
        self.assertEqual(spaces.uri_tree.find_node(uri).process_all['get'], (dummy_method_name_get, dummy_spaces_context, sample_kwargs))
        self.assertEqual(spaces.uri_tree.find_node(uri).process_all['put'], (dummy_method_name_put, dummy_spaces_context, sample_kwargs))
        self.assertEqual(spaces.uri_tree.find_node(uri).process_all['delete'], (dummy_method_name_delete, dummy_spaces_context, sample_kwargs))
        self.assertEqual(spaces.uri_tree.find_node(uri).process_all['find'], (dummy_method_name_find, dummy_spaces_context, sample_kwargs))  
        self.assertEqual(spaces.uri_tree.find_node(uri).process_all['remove'], (dummy_method_name_remove, dummy_spaces_context, sample_kwargs))          
    
    def test_delete_process(self):
        '''[SpacesContext] delete process'''
        dummy_base = 'http://dummy.com'
        dummy_process_method_name_get = 'dummy_process_method_name_get'
        dummy_process_method_name_put = 'dummy_process_method_name_put'
        dummy_process_method_name_delete = 'dummy_process_method_name_delete'
        dummy_process_method_name_find = 'dummy_process_method_name_find'
        dummy_process_method_name_remove = 'dummy_method_name_remove'
        dummy_process_all_method_name_get = 'dummy_process_all_method_name_get'
        dummy_process_all_method_name_put = 'dummy_process_all_method_name_put'
        dummy_process_all_method_name_delete = 'dummy_process_all_method_name_delete'
        dummy_process_all_method_name_find = 'dummy_process_all_method_name_find'
        dummy_process_all_method_name_remove = 'dummy_method_name_remove'
        uri = Uri(dummy_base).collection(True)
        spaces = Spaces()
        dummy_spaces_queue = Queue()
        dummy_spaces_context = SpacesContext(spaces, dummy_spaces_queue)          
        process_ref_1 = dummy_spaces_context.process_all(uri,   
            get=dummy_process_all_method_name_get, 
            put=dummy_process_all_method_name_put, 
            delete=dummy_process_all_method_name_delete, 
            find=dummy_process_all_method_name_find, 
            remove=dummy_process_all_method_name_remove
            )
        process_ref_2 = dummy_spaces_context.process(uri,    
            get=dummy_process_method_name_get, 
            put=dummy_process_method_name_put, 
            delete=dummy_process_method_name_delete, 
            find=dummy_process_method_name_find, 
            remove=dummy_process_method_name_remove
            )
        self.assertEqual(spaces.uri_tree.find_node(uri).process_all['get'], (dummy_process_all_method_name_get, dummy_spaces_context, {}))
        self.assertEqual(spaces.uri_tree.find_node(uri).process['put'], (dummy_process_method_name_put, dummy_spaces_context, {}))
        self.assertEqual(spaces.delete_process(process_ref_1), None)
        self.assertFalse('get' in spaces.uri_tree.find_node(uri).process_all)
        self.assertEqual(spaces.uri_tree.find_node(uri).process['put'], (dummy_process_method_name_put, dummy_spaces_context, {}))
        self.assertEqual(dummy_spaces_context.delete_process(process_ref_2), None)
        self.assertFalse('get' in spaces.uri_tree.find_node(uri).process_all)
        self.assertTrue(len(spaces.uri_tree.find_node(uri).process) == 0)      
        
class SpacesTest(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_get_case_1(self):
        '''[Spaces] test_get no callback with no process'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        node_1 = Node('dummyuri')
        node_1.resource = dummy_resource
        spaces.uri_tree.add_node(node_1, uri)
        sample_kwargs = {'one':1, 'two':2}  
        self.assertEqual(spaces.get(uri.dummyuri, dummy_spaces_context, **sample_kwargs), dummy_resource)

    def test_get_case_2(self):
        '''[Spaces] test_get no callback with process on uri'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        process_dummy_queue = Queue()
        process_dummy_method_name = 'process_dummy_method_name'        
        process_dummy_spaces_context = SpacesContext(spaces, process_dummy_queue)
        sample_kwargs = {'one':1, 'two':2}                         
        node = Node('dummyuri')
        node.process['get'] = (process_dummy_method_name, process_dummy_spaces_context, sample_kwargs)  
        spaces.uri_tree.add_node(node, uri) 
        def emulate_task_response():
            (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = process_dummy_queue.get(True)
            self.assertEqual(return_value, None)
            self.assertEqual(process_callback, process_dummy_method_name)
            self.assertEqual(method, 'get')
            self.assertEqual(called_uri, uri.dummyuri)
            self.assertEqual(result, None)
            self.assertEqual(kwargs, sample_kwargs)
            spaces_context.return_from_callback(spaces_context, return_value=dummy_resource)
        thread.start_new_thread(emulate_task_response, ())
        self.assertEqual(spaces.get(uri.dummyuri, dummy_spaces_context), dummy_resource)

    def test_get_case_3(self):
        '''[Spaces] test_get no callback with process_all on parent uri'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        process_dummy_queue = Queue()
        process_dummy_method_name = 'process_dummy_method_name'        
        process_dummy_spaces_context = SpacesContext(spaces, process_dummy_queue)  
        dummy_resource = Resource()
        sample_kwargs = {'one':1, 'two':2}
        node = Node('dummyuri/')
        node.process_all['get'] = (process_dummy_method_name, process_dummy_spaces_context, sample_kwargs)  
        spaces.uri_tree.add_node(node, uri)        
        def emulate_task_response():
            (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = process_dummy_queue.get(True)
            self.assertEqual(return_value, None)
            self.assertEqual(process_callback, process_dummy_method_name)
            self.assertEqual(method, 'get')
            self.assertEqual(called_uri, uri.dummyuri.funnyuri.sillyuri)
            self.assertEqual(result, None)
            self.assertEqual(kwargs, sample_kwargs)
            spaces_context.return_from_callback(spaces_context, return_value=dummy_resource)
        thread.start_new_thread(emulate_task_response, ())
        self.assertEqual(spaces.get(uri.dummyuri.funnyuri.sillyuri, dummy_spaces_context), dummy_resource)        
        
    def test_get_case_4(self):
        '''[Spaces] test_get with callback with no process'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue) 
        dummy_method_name = 'dummy_method_name'
        node = Node('dummyuri')
        node.resource = dummy_resource
        spaces.uri_tree.add_node(node, uri)
        sample_kwargs = {'one':1, 'two':2}
        self.assertEqual(spaces.get(uri.dummyuri, dummy_spaces_context, dummy_method_name, **sample_kwargs), None)
        self.assertEqual(dummy_queue.get(True), (dummy_method_name, 'get', uri.dummyuri, dummy_resource, None, dummy_spaces_context, sample_kwargs))

    def test_get_case_5(self):
        '''[Spaces] test_get with callback with process'''
        dummy_base = 'http://dummy.com'
        dummy_resource = Resource()
        uri = Uri(dummy_base).collection(True)
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'
        process_dummy_queue = Queue()
        process_dummy_method_name = 'dummy_method_name'          
        spaces = Spaces()
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        process_dummy_spaces_context = SpacesContext(spaces, process_dummy_queue)
        sample_kwargs_1 = {'one':1, 'two':2}
        sample_kwargs_2 = {'three':3}
        node = Node('dummyuri')
        node.process['get'] = (process_dummy_method_name, process_dummy_spaces_context, sample_kwargs_1)       
        spaces.uri_tree.add_node(node, uri)
        def emulate_task_response():
            (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = process_dummy_queue.get(True)
            self.assertEqual(return_value, None)
            self.assertEqual(process_callback, process_dummy_method_name)
            self.assertEqual(method, 'get')
            self.assertEqual(called_uri, uri.dummyuri)
            self.assertEqual(result, None)
            self.assertEqual(kwargs, sample_kwargs_1)
            spaces.return_from_callback(spaces_context, dummy_resource)    
        thread.start_new_thread(emulate_task_response, ())        
        self.assertEqual(spaces.get(uri.dummyuri, dummy_spaces_context, dummy_method_name, **sample_kwargs_2), None)
        (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = dummy_queue.get(True)
        self.assertEqual(process_callback, dummy_method_name)
        self.assertEqual(method, 'get')
        self.assertEqual(called_uri, uri.dummyuri)
        self.assertEqual(return_value, dummy_resource)
        self.assertEqual(result, None)
        self.assertEqual(spaces_context, dummy_spaces_context)
        self.assertEqual(kwargs, sample_kwargs_2)

    def test_get_case_6(self):
        '''[Spaces] test_get no callback with no process and watches'''
        dummy_base = 'http://dummy.com'
        dummy_uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)        
        watch_dummy_queue_1 = Queue()
        watch_dummy_spaces_context_1 = SpacesContext(spaces, watch_dummy_queue_1)
        watch_dummy_method_name_1 = 'watch_dummy_method_name_1'
        watch_dummy_queue_2 = Queue()
        watch_dummy_spaces_context_2 = SpacesContext(spaces, watch_dummy_queue_2)
        watch_dummy_method_name_2 = 'watch_dummy_method_name_2'
        sample_kwargs_1 = {'one':1, 'two':2}
        sample_kwargs_2 = {'three':3}
        node_1 = Node('dummyuri/')
        node_1.watch_all['get'] = (watch_dummy_method_name_1, watch_dummy_spaces_context_1, sample_kwargs_1)
        node_2 = Node('funnyuri')
        node_2.resource = dummy_resource
        node_2.watch['get'] = (watch_dummy_method_name_2, watch_dummy_spaces_context_2, sample_kwargs_2)
        spaces.uri_tree.add_node(node_1, dummy_uri)
        spaces.uri_tree.add_node(node_2, dummy_uri.dummyuri.collection(True))
        self.assertEqual(spaces.get(dummy_uri.dummyuri.funnyuri, dummy_spaces_context), dummy_resource)
        (watch_dummy_method_name, method, uri, res, result, context, kwargs) = watch_dummy_queue_1.get(True)
        self.assertEqual(watch_dummy_method_name, watch_dummy_method_name_1)
        self.assertEqual(method, 'get')
        self.assertEqual(uri, dummy_uri.dummyuri.funnyuri)
        self.assertEqual(res, dummy_resource)
        self.assertEqual(result, None)
        self.assertEqual(kwargs, sample_kwargs_1)
        self.assertNotEqual(context, watch_dummy_spaces_context_1)
        (watch_dummy_method_name, method, uri, res, result, context, kwargs) = watch_dummy_queue_2.get(True)
        self.assertEqual(watch_dummy_method_name, watch_dummy_method_name_2)
        self.assertEqual(method, 'get')
        self.assertEqual(uri, dummy_uri.dummyuri.funnyuri)
        self.assertEqual(res, dummy_resource)
        self.assertEqual(result, None)
        self.assertEqual(kwargs, sample_kwargs_2)
        self.assertNotEqual(context, watch_dummy_spaces_context_2)        

    def test_get_case_7(self):
        '''[Spaces] test_get no callback with process on uri with exception'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        process_dummy_queue = Queue()
        process_dummy_method_name = 'process_dummy_method_name'        
        process_dummy_spaces_context = SpacesContext(spaces, process_dummy_queue)
        sample_kwargs_1 = {'one':1, 'two':2}
        sample_kwargs_2 = {'three':3}              
        node = Node('dummyuri')
        node.process['get'] = (process_dummy_method_name, process_dummy_spaces_context, sample_kwargs_1)  
        spaces.uri_tree.add_node(node, uri)        
        def emulate_task_response():
            (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = process_dummy_queue.get(True)
            self.assertEqual(return_value, None)
            self.assertEqual(process_callback, process_dummy_method_name)
            self.assertEqual(method, 'get')
            self.assertEqual(called_uri, uri.dummyuri)
            self.assertEqual(result, None)
            self.assertEqual(kwargs, sample_kwargs_1)
            spaces_context.return_from_callback(spaces_context, return_value=None, exception=Exception())
        thread.start_new_thread(emulate_task_response, ())
        self.assertRaises(Exception, spaces.get, uri.dummyuri, dummy_spaces_context, **sample_kwargs_2)

    def test_get_case_8(self):
        '''[Spaces] test_get with callback with process with exception'''
        dummy_base = 'http://dummy.com'
        dummy_resource = Resource()
        uri = Uri(dummy_base).collection(True)
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'
        process_dummy_queue = Queue()
        process_dummy_method_name = 'dummy_method_name'
        e = Exception()
        spaces = Spaces()
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        process_dummy_spaces_context = SpacesContext(spaces, process_dummy_queue)
        sample_kwargs_1 = {'one':1, 'two':2}
        sample_kwargs_2 = {'three':3}      
        node = Node('dummyuri')
        node.process['get'] = (process_dummy_method_name, process_dummy_spaces_context, sample_kwargs_1)       
        spaces.uri_tree.add_node(node, uri)
        def emulate_task_response():
            (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = process_dummy_queue.get(True)
            self.assertEqual(return_value, None)
            self.assertEqual(process_callback, process_dummy_method_name)
            self.assertEqual(method, 'get')
            self.assertEqual(called_uri, uri.dummyuri)
            self.assertEqual(result, None)
            self.assertEqual(kwargs, sample_kwargs_1)
            spaces.return_from_callback(spaces_context, exception=e)    
        thread.start_new_thread(emulate_task_response, ())        
        self.assertEqual(spaces.get(uri.dummyuri, dummy_spaces_context, dummy_method_name, **sample_kwargs_2), None)
        (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = dummy_queue.get(True)
        self.assertEqual(process_callback, dummy_method_name)
        self.assertEqual(method, 'get')
        self.assertEqual(called_uri, uri.dummyuri)
        self.assertEqual(return_value, None)
        self.assertEqual(result, e)
        self.assertEqual(spaces_context, dummy_spaces_context)
        self.assertEqual(kwargs, sample_kwargs_2)
    
    def test_get_case_9(self):
        '''[Spaces] test get with wait with no callback where resource is already there.'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        node_1 = Node('dummyuri')
        node_1.resource = dummy_resource
        spaces.uri_tree.add_node(node_1, uri)
        sample_kwargs = {'one':1, 'two':2}        
        self.assertEqual(spaces.get(uri.dummyuri, dummy_spaces_context, wait=True, **sample_kwargs), dummy_resource)
                
    def test_get_case_10(self):
        '''[Spaces] test get with wait with no callback and waiting for resource to be added.'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        node_1 = Node('dummyuri')
        sample_kwargs = {'one':1, 'two':2} 
        def delayed_put_to_spaces():
            time.sleep(2)
            put_queue = Queue()
            put_spaces_context = SpacesContext(spaces, put_queue)
            put_spaces_context.put(uri.dummyuri, dummy_resource)
        spaces.uri_tree.add_node(node_1, uri)
        thread.start_new_thread(delayed_put_to_spaces, ())
        self.assertEqual(spaces.get(uri.dummyuri, dummy_spaces_context, wait=True, **sample_kwargs), dummy_resource)
        
    def test_get_case_11(self):
        '''[Spaces] test get with wait with callback where resource is already there.'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        node_1 = Node('dummyuri')
        node_1.resource = dummy_resource
        spaces.uri_tree.add_node(node_1, uri)
        sample_kwargs = {'one':1, 'two':2} 
        self.assertEqual(spaces.get(uri.dummyuri, dummy_spaces_context, callback='dummy_callback', wait=True, **sample_kwargs), None)
        self.assertEqual(dummy_queue.get(True), ('dummy_callback', 'get', uri.dummyuri, dummy_resource, None, dummy_spaces_context, sample_kwargs))
                
    def test_get_case_12(self):
        '''[Spaces] test get with wait with callback and waiting for resource to be added.'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        node_1 = Node('dummyuri')
        sample_kwargs = {'one':1, 'two':2} 
        def delayed_put_to_spaces():
            time.sleep(2)
            put_queue = Queue()
            put_spaces_context = SpacesContext(spaces, put_queue)
            put_spaces_context.put(uri.dummyuri, dummy_resource)
        spaces.uri_tree.add_node(node_1, uri)
        thread.start_new_thread(delayed_put_to_spaces, ())
        self.assertEqual(spaces.get(uri.dummyuri, dummy_spaces_context, callback='dummy_callback', wait=True, **sample_kwargs), None)
        self.assertEqual(dummy_queue.get(True), ('dummy_callback', 'put', uri.dummyuri, dummy_resource, None, dummy_spaces_context, sample_kwargs))
                                
    def test_put_case_1(self):
        '''[Spaces] test_put no callback, no process'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()        
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)     
        sample_kwargs = {'one':1, 'two':2} 
        spaces.put(uri.dummyuri, dummy_resource, dummy_spaces_context, **sample_kwargs)
        self.assertEqual(spaces.uri_tree.find_node(uri.dummyuri).resource, dummy_resource)
    
    def test_put_case_2(self):
        '''[Spaces] test_put put to collection, no callback, no process'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()        
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        sample_kwargs = {'one':1, 'two':2} 
        new_uri = spaces.put(uri.dummyuri.collection(), dummy_resource, dummy_spaces_context, **sample_kwargs)
        self.assertNotEqual(uri.dummyuri, new_uri, dummy_spaces_context)
        self.assertEqual(spaces.uri_tree.find_node(new_uri).resource, dummy_resource)
        
    def test_put_case_3(self):
        '''[Spaces] test_put with callback, no process'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'
        sample_kwargs = {'one':1, 'two':2} 
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        self.assertEqual(spaces.put(uri.dummyuri, dummy_resource, dummy_spaces_context, dummy_method_name, **sample_kwargs), None)
        self.assertEqual(dummy_queue.get(True), (dummy_method_name, 'put', uri.dummyuri, uri.dummyuri, None, dummy_spaces_context, sample_kwargs))
    
    def test_put_case_4(self):
        '''[Spaces] test_put put to collection, no callback, with process'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()    
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        process_dummy_queue = Queue()
        process_dummy_method_name = 'process_dummy_method_name'        
        process_dummy_spaces_context = SpacesContext(spaces, process_dummy_queue) 
        sample_kwargs_1 = {'one':1, 'two':2} 
        sample_kwargs_2 = {'three':3}             
        node = Node('dummyuri/')
        node.process['put'] = (process_dummy_method_name, process_dummy_spaces_context, sample_kwargs_1)  
        spaces.uri_tree.add_node(node, uri)          
        def emulate_task_response():
            (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = process_dummy_queue.get(True)
            self.assertEqual(process_callback, process_dummy_method_name)
            self.assertEqual(method, 'put')
            self.assertEqual(called_uri, uri.dummyuri.collection())
            self.assertEqual(return_value, dummy_resource)
            self.assertEqual(result, None)
            self.assertEqual(kwargs, sample_kwargs_1)
            spaces_context.return_from_callback(spaces_context, return_value=uri.dummyuri.blah)
        thread.start_new_thread(emulate_task_response, ())
        self.assertEqual(spaces.put(uri.dummyuri.collection(), dummy_resource, dummy_spaces_context, **sample_kwargs_2), uri.dummyuri.blah)                

    def test_put_case_5(self):
        '''[Spaces] test_put put to collection, with callback, with process'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()    
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        process_dummy_queue = Queue()
        process_dummy_method_name = 'process_dummy_method_name'        
        process_dummy_spaces_context = SpacesContext(spaces, process_dummy_queue)
        sample_kwargs_1 = {'one':1, 'two':2} 
        sample_kwargs_2 = {'three':3}            
        node = Node('dummyuri/')
        node.process['put'] = (process_dummy_method_name, process_dummy_spaces_context, sample_kwargs_1)   
        spaces = Spaces()
        spaces.uri_tree.add_node(node, uri)
        def emulate_task_response():
            (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = process_dummy_queue.get(True)
            self.assertEqual(return_value, dummy_resource)
            self.assertEqual(process_callback, process_dummy_method_name)
            self.assertEqual(method, 'put')
            self.assertEqual(called_uri, uri.dummyuri)
            self.assertEqual(result, None)
            self.assertEqual(kwargs, sample_kwargs_1)
            spaces.return_from_callback(spaces_context, uri.dummyuri.blah)    
        thread.start_new_thread(emulate_task_response, ())         
        self.assertEqual(spaces.put(uri.dummyuri.collection(), dummy_resource, dummy_spaces_context, dummy_method_name, **sample_kwargs_2), None)
        (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = dummy_queue.get(True)
        self.assertEqual(process_callback, dummy_method_name)
        self.assertEqual(method, 'put')
        self.assertEqual(called_uri, uri.dummyuri.collection())
        self.assertEqual(return_value, uri.dummyuri.blah)
        self.assertEqual(result, None) 
        self.assertEqual(spaces_context, dummy_spaces_context)
        self.assertEqual(kwargs, sample_kwargs_2)
        
    def test_put_case_6(self):
        '''[Spaces] test_put no callback with no process and watches'''
        dummy_base = 'http://dummy.com'
        dummy_uri = Uri(dummy_base).collection(True)
        spaces = Spaces()
        dummy_resource = Resource()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)        
        watch_dummy_queue_1 = Queue()
        watch_dummy_spaces_context_1 = SpacesContext(spaces, watch_dummy_queue_1)
        watch_dummy_method_name_1 = 'watch_dummy_method_name_1'
        watch_dummy_queue_2 = Queue()
        watch_dummy_spaces_context_2 = SpacesContext(spaces, watch_dummy_queue_2)
        watch_dummy_method_name_2 = 'watch_dummy_method_name_2'
        sample_kwargs_1 = {'one':1, 'two':2} 
        sample_kwargs_2 = {'three':3}  
        sample_kwargs_3 = {'four':4}
        node_1 = Node('dummyuri/')
        node_1.watch_all['put'] = (watch_dummy_method_name_1, watch_dummy_spaces_context_1, sample_kwargs_1)
        node_2 = Node('funnyuri')
        node_2.resource = dummy_resource
        node_2.watch['put'] = (watch_dummy_method_name_2, watch_dummy_spaces_context_2, sample_kwargs_2)
        spaces.uri_tree.add_node(node_1, dummy_uri)
        spaces.uri_tree.add_node(node_2, dummy_uri.dummyuri.collection(True))
        self.assertEqual(spaces.put(dummy_uri.dummyuri.funnyuri, dummy_resource, dummy_spaces_context, **sample_kwargs_3), dummy_uri.dummyuri.funnyuri)
        (watch_dummy_method_name, method, uri, res, result, context, kwargs) = watch_dummy_queue_1.get(True)
        self.assertEqual(watch_dummy_method_name, watch_dummy_method_name_1)
        self.assertEqual(method, 'put')
        self.assertEqual(uri, dummy_uri.dummyuri.funnyuri)
        self.assertEqual(res, dummy_uri.dummyuri.funnyuri)
        self.assertEqual(result, None)
        self.assertEqual(kwargs, sample_kwargs_1)
        self.assertNotEqual(context, watch_dummy_spaces_context_1)
        (watch_dummy_method_name, method, uri, res, result, context, kwargs) = watch_dummy_queue_2.get(True)
        self.assertEqual(watch_dummy_method_name, watch_dummy_method_name_2)
        self.assertEqual(method, 'put')
        self.assertEqual(uri, dummy_uri.dummyuri.funnyuri)
        self.assertEqual(res, dummy_uri.dummyuri.funnyuri)
        self.assertEqual(result, None)
        self.assertEqual(kwargs, sample_kwargs_2)
        self.assertNotEqual(context, watch_dummy_spaces_context_2)          

    def test_put_case_7(self):
        '''[Spaces] test_put put to collection, no callback, with process with exception'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()    
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        process_dummy_queue = Queue()
        process_dummy_method_name = 'process_dummy_method_name'        
        process_dummy_spaces_context = SpacesContext(spaces, process_dummy_queue)   
        sample_kwargs_1 = {'one':1, 'two':2} 
        sample_kwargs_2 = {'three':3}                    
        node = Node('dummyuri/')
        node.process['put'] = (process_dummy_method_name, process_dummy_spaces_context, sample_kwargs_1)  
        spaces.uri_tree.add_node(node, uri)          
        def emulate_task_response():
            (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = process_dummy_queue.get(True)
            self.assertEqual(process_callback, process_dummy_method_name)
            self.assertEqual(method, 'put')
            self.assertEqual(called_uri, uri.dummyuri.collection())
            self.assertEqual(return_value, dummy_resource)
            self.assertEqual(result, None)
            self.assertEqual(kwargs, sample_kwargs_1)
            spaces_context.return_from_callback(spaces_context, return_value=None, exception=Exception())
        thread.start_new_thread(emulate_task_response, ())
        self.assertRaises(Exception, spaces.put, uri.dummyuri.collection(), dummy_resource, dummy_spaces_context, **sample_kwargs_2)

    def test_put_case_8(self):
        '''[Spaces] test_put put to collection, with callback, with process with exception'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()    
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'
        e = Exception()        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        process_dummy_queue = Queue()
        process_dummy_method_name = 'process_dummy_method_name'        
        process_dummy_spaces_context = SpacesContext(spaces, process_dummy_queue)
        sample_kwargs_1 = {'one':1, 'two':2} 
        sample_kwargs_2 = {'three':3}            
        node = Node('dummyuri/')
        node.process['put'] = (process_dummy_method_name, process_dummy_spaces_context, sample_kwargs_1)   
        spaces = Spaces()
        spaces.uri_tree.add_node(node, uri)
        def emulate_task_response():
            (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = process_dummy_queue.get(True)
            self.assertEqual(return_value, dummy_resource)
            self.assertEqual(process_callback, process_dummy_method_name)
            self.assertEqual(method, 'put')
            self.assertEqual(called_uri, uri.dummyuri.collection(True))
            self.assertEqual(result, None)
            self.assertEqual(kwargs, sample_kwargs_1)
            spaces.return_from_callback(spaces_context, exception=e)    
        thread.start_new_thread(emulate_task_response, ())         
        self.assertEqual(spaces.put(uri.dummyuri.collection(), dummy_resource, dummy_spaces_context, dummy_method_name, **sample_kwargs_2), None)
        (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = dummy_queue.get(True)
        self.assertEqual(process_callback, dummy_method_name)
        self.assertEqual(method, 'put')
        self.assertEqual(called_uri, uri.dummyuri.collection())
        self.assertEqual(return_value, None)
        self.assertEqual(result, e) 
        self.assertEqual(spaces_context, dummy_spaces_context)
        self.assertEqual(kwargs, sample_kwargs_2)
                                                 
    def test_delete_case_1(self):
        '''[Spaces] test_delete no callback, no process'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        node = Node('dummyuri')
        node.resource = dummy_resource
        spaces.uri_tree.add_node(node, uri)    
        sample_kwargs = {'one':1, 'two':2}     
        self.assertEqual(spaces.delete(uri.dummyuri, dummy_spaces_context, **sample_kwargs), None)
        self.assertNotEqual(spaces.uri_tree.find_node(uri.dummyuri), dummy_resource)
        
    def test_delete_case_2(self):
        '''[Spaces] test_delete with callback, no process'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)    
        node = Node('dummyuri')
        node.resource = dummy_resource
        spaces.uri_tree.add_node(node, uri)
        sample_kwargs = {'one':1, 'two':2}           
        self.assertEqual(spaces.delete(uri.dummyuri, dummy_spaces_context, dummy_method_name, **sample_kwargs), None)
        (callback_method_name, method, called_uri, return_value, result, caller_spaces_context, kwargs) = dummy_queue.get(True)
        self.assertEqual(callback_method_name, dummy_method_name)
        self.assertEqual(method, 'delete')
        self.assertEqual(result, None)  
        self.assertEqual(return_value, None)
        self.assertEqual(caller_spaces_context, dummy_spaces_context)
        self.assertEqual(called_uri, uri.dummyuri)
        self.assertEqual(kwargs, sample_kwargs)         
    
    def test_delete_case_3(self):
        '''[Spaces] test_delete no callback, with process'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()    
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        process_dummy_queue = Queue()
        process_dummy_method_name = 'process_dummy_method_name'        
        process_dummy_spaces_context = SpacesContext(spaces, process_dummy_queue)         
        sample_kwargs_1 = {'one':1, 'two':2} 
        sample_kwargs_2 = {'three':3}                  
        node = Node('dummyuri')
        node.process['delete'] = (process_dummy_method_name, process_dummy_spaces_context, sample_kwargs_1)  
        spaces.uri_tree.add_node(node, uri)          
        def emulate_task_response():
            (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = process_dummy_queue.get(True)
            self.assertEqual(process_callback, process_dummy_method_name)
            self.assertEqual(method, 'delete')
            self.assertEqual(called_uri, uri.dummyuri)
            self.assertEqual(result, None)
            self.assertEqual(return_value, None)
            self.assertEqual(kwargs, sample_kwargs_1)
            spaces_context.return_from_callback(spaces_context, return_value=dummy_resource)
        thread.start_new_thread(emulate_task_response, ())
        self.assertEqual(spaces.delete(uri.dummyuri, dummy_spaces_context, **sample_kwargs_2), dummy_resource)

    def test_delete_case_4(self):
        '''[Spaces] test_delete with callback, with process'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()    
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        process_dummy_queue = Queue()
        process_dummy_method_name = 'process_dummy_method_name'        
        process_dummy_spaces_context = SpacesContext(spaces, process_dummy_queue)
        sample_kwargs_1 = {'one':1, 'two':2} 
        sample_kwargs_2 = {'three':3}         
        node = Node('dummyuri')
        node.process['delete'] = (process_dummy_method_name, process_dummy_spaces_context, sample_kwargs_1)          
        spaces.uri_tree.add_node(node, uri)
        def emulate_task_response():
            (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = process_dummy_queue.get(True)
            self.assertEqual(return_value, None)
            self.assertEqual(process_callback, process_dummy_method_name)
            self.assertEqual(method, 'delete')
            self.assertEqual(called_uri, uri.dummyuri)
            self.assertEqual(result, None)
            self.assertEqual(kwargs, sample_kwargs_1)
            spaces.return_from_callback(spaces_context, None)    
        thread.start_new_thread(emulate_task_response, ())               
        self.assertEqual(spaces.delete(uri.dummyuri, dummy_spaces_context, dummy_method_name, **sample_kwargs_2), None)
        (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = dummy_queue.get(True)
        self.assertEqual(return_value, None)
        self.assertEqual(process_callback, dummy_method_name)
        self.assertEqual(method, 'delete')
        self.assertEqual(called_uri, uri.dummyuri)
        self.assertEqual(result, None) 
        self.assertEqual(spaces_context, dummy_spaces_context)
        self.assertEqual(kwargs, sample_kwargs_2)

    def test_delete_case_5(self):
        '''[Spaces] test_delete no callback, with process with exception'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()    
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        process_dummy_queue = Queue()
        process_dummy_method_name = 'process_dummy_method_name'        
        process_dummy_spaces_context = SpacesContext(spaces, process_dummy_queue)
        sample_kwargs_1 = {'one':1, 'two':2} 
        sample_kwargs_2 = {'three':3}                         
        node = Node('dummyuri')
        node.process['delete'] = (process_dummy_method_name, process_dummy_spaces_context, sample_kwargs_1)  
        spaces.uri_tree.add_node(node, uri)          
        def emulate_task_response():
            (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = process_dummy_queue.get(True)
            self.assertEqual(process_callback, process_dummy_method_name)
            self.assertEqual(method, 'delete')
            self.assertEqual(called_uri, uri.dummyuri)
            self.assertEqual(result, None)
            self.assertEqual(return_value, None)
            self.assertEqual(kwargs, sample_kwargs_1)
            spaces_context.return_from_callback(spaces_context, return_value=None, exception=Exception())
        thread.start_new_thread(emulate_task_response, ())
        self.assertRaises(Exception, spaces.delete, uri.dummyuri, dummy_spaces_context, **sample_kwargs_2)

    def test_delete_case_6(self):
        '''[Spaces] test_delete with callback, with process with exception'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()    
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        process_dummy_queue = Queue()
        process_dummy_method_name = 'process_dummy_method_name'
        e = Exception()        
        process_dummy_spaces_context = SpacesContext(spaces, process_dummy_queue)
        sample_kwargs_1 = {'one':1, 'two':2} 
        sample_kwargs_2 = {'three':3}                 
        node = Node('dummyuri')
        node.process['delete'] = (process_dummy_method_name, process_dummy_spaces_context, sample_kwargs_1)          
        spaces.uri_tree.add_node(node, uri)
        def emulate_task_response():
            (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = process_dummy_queue.get(True)
            self.assertEqual(return_value, None)
            self.assertEqual(process_callback, process_dummy_method_name)
            self.assertEqual(method, 'delete')
            self.assertEqual(called_uri, uri.dummyuri)
            self.assertEqual(result, None)
            self.assertEqual(kwargs, sample_kwargs_1)
            spaces.return_from_callback(spaces_context, exception=e)    
        thread.start_new_thread(emulate_task_response, ())               
        self.assertEqual(spaces.delete(uri.dummyuri, dummy_spaces_context, dummy_method_name, **sample_kwargs_2), None)
        (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = dummy_queue.get(True)
        self.assertEqual(return_value, None)
        self.assertEqual(process_callback, dummy_method_name)
        self.assertEqual(method, 'delete')
        self.assertEqual(called_uri, uri.dummyuri)
        self.assertEqual(result, e) 
        self.assertEqual(spaces_context, dummy_spaces_context)
        self.assertEqual(kwargs, sample_kwargs_2)
                
    def test_remove_case_1(self):
        '''[Spaces] test_remove no callback, no process'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        node = Node('dummyuri')
        node.resource = dummy_resource
        spaces.uri_tree.add_node(node, uri)    
        sample_kwargs = {'one':1, 'two':2}             
        self.assertEqual(spaces.remove(uri.dummyuri, dummy_spaces_context, **sample_kwargs), dummy_resource)
        self.assertNotEqual(spaces.uri_tree.find_node(uri.dummyuri), dummy_resource)
        
    def test_remove_case_2(self):
        '''[Spaces] test_remove with callback, no process'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        node = Node('dummyuri')
        node.resource = dummy_resource
        spaces.uri_tree.add_node(node, uri)
        sample_kwargs = {'one':1, 'two':2}     
        self.assertEqual(spaces.remove(uri.dummyuri, dummy_spaces_context, dummy_method_name, **sample_kwargs), None)
        (callback_method_name, method, uri.dummyuri, return_value, result, caller_spaces_context, kwargs) = dummy_queue.get(True)
        self.assertEqual(callback_method_name, dummy_method_name)
        self.assertEqual(method, 'remove')
        self.assertEqual(result, None)  
        self.assertEqual(return_value, dummy_resource)
        self.assertEqual(caller_spaces_context, dummy_spaces_context)
        self.assertEqual(kwargs, sample_kwargs)
    
    def test_remove_case_3(self):
        '''[Spaces] test_remove no callback, with process'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        process_dummy_queue = Queue()
        process_dummy_method_name = 'process_dummy_method_name'        
        process_dummy_spaces_context = SpacesContext(spaces, process_dummy_queue)    
        sample_kwargs_1 = {'one':1, 'two':2} 
        sample_kwargs_2 = {'three':3}               
        node = Node('dummyuri')
        node.process['remove'] = (process_dummy_method_name, process_dummy_spaces_context, sample_kwargs_1)        
        spaces.uri_tree.add_node(node, uri)          
        def emulate_task_response():
            (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = process_dummy_queue.get(True)
            self.assertEqual(process_callback, process_dummy_method_name)
            self.assertEqual(method, 'remove')
            self.assertEqual(called_uri, uri.dummyuri)
            self.assertEqual(result, None)      
            self.assertEqual(return_value, None)           
            self.assertEqual(kwargs, sample_kwargs_1)
            spaces_context.return_from_callback(spaces_context, return_value=dummy_resource)
        thread.start_new_thread(emulate_task_response, ())
        self.assertEqual(spaces.remove(uri.dummyuri, dummy_spaces_context, **sample_kwargs_2), dummy_resource)

    def test_remove_case_4(self):
        '''[Spaces] test_remove with callback, with process'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()    
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        process_dummy_queue = Queue()
        process_dummy_method_name = 'process_dummy_method_name'        
        process_dummy_spaces_context = SpacesContext(spaces, process_dummy_queue)
        sample_kwargs_1 = {'one':1, 'two':2} 
        sample_kwargs_2 = {'three':3}                   
        node = Node('dummyuri')
        node.process['remove'] = (process_dummy_method_name, process_dummy_spaces_context, sample_kwargs_1)    
        spaces = Spaces()
        spaces.uri_tree.add_node(node, uri)
        def emulate_task_response():
            (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = process_dummy_queue.get(True)
            self.assertEqual(return_value, None)
            self.assertEqual(process_callback, process_dummy_method_name)
            self.assertEqual(method, 'remove')
            self.assertEqual(called_uri, uri.dummyuri)
            self.assertEqual(result, None)
            self.assertEqual(kwargs, sample_kwargs_1)
            spaces.return_from_callback(spaces_context, dummy_resource)    
        thread.start_new_thread(emulate_task_response, ())               
        self.assertEqual(spaces.remove(uri.dummyuri, dummy_spaces_context, dummy_method_name, **sample_kwargs_2), None)
        (callback_method, method, called_uri, return_value, result, spaces_context, kwargs) = dummy_queue.get(True)
        self.assertEqual(callback_method, dummy_method_name)
        self.assertEqual(method, 'remove')
        self.assertEqual(called_uri, uri.dummyuri)
        self.assertEqual(result, None) 
        self.assertEqual(return_value, dummy_resource)
        self.assertEqual(spaces_context, dummy_spaces_context)
        self.assertEqual(kwargs, sample_kwargs_2)

    def test_remove_case_5(self):
        '''[Spaces] test_remove no callback, with process'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        process_dummy_queue = Queue()
        process_dummy_method_name = 'process_dummy_method_name'        
        process_dummy_spaces_context = SpacesContext(spaces, process_dummy_queue)
        sample_kwargs_1 = {'one':1, 'two':2} 
        sample_kwargs_2 = {'three':3}              
        node = Node('dummyuri')
        node.process['remove'] = (process_dummy_method_name, process_dummy_spaces_context, sample_kwargs_1)        
        spaces.uri_tree.add_node(node, uri)          
        def emulate_task_response():
            (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = process_dummy_queue.get(True)
            self.assertEqual(process_callback, process_dummy_method_name)
            self.assertEqual(method, 'remove')
            self.assertEqual(called_uri, uri.dummyuri)
            self.assertEqual(result, None)      
            self.assertEqual(return_value, None)
            self.assertEqual(kwargs, sample_kwargs_1)           
            spaces_context.return_from_callback(spaces_context, return_value=None, exception=Exception())
        thread.start_new_thread(emulate_task_response, ())
        self.assertRaises(Exception, spaces.remove, uri.dummyuri, dummy_spaces_context, **sample_kwargs_2)

    def test_remove_case_6(self):
        '''[Spaces] test_remove with callback, with process'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()    
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        process_dummy_queue = Queue()
        process_dummy_method_name = 'process_dummy_method_name'        
        process_dummy_spaces_context = SpacesContext(spaces, process_dummy_queue)
        sample_kwargs_1 = {'one':1, 'two':2} 
        sample_kwargs_2 = {'three':3}          
        e = Exception()        
        node = Node('dummyuri')
        node.process['remove'] = (process_dummy_method_name, process_dummy_spaces_context, sample_kwargs_1)    
        spaces = Spaces()
        spaces.uri_tree.add_node(node, uri)
        def emulate_task_response():
            (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = process_dummy_queue.get(True)
            self.assertEqual(return_value, None)
            self.assertEqual(process_callback, process_dummy_method_name)
            self.assertEqual(method, 'remove')
            self.assertEqual(called_uri, uri.dummyuri)
            self.assertEqual(result, None)
            self.assertEqual(kwargs, sample_kwargs_1)
            spaces.return_from_callback(spaces_context, exception=e)    
        thread.start_new_thread(emulate_task_response, ())               
        self.assertEqual(spaces.remove(uri.dummyuri, dummy_spaces_context, dummy_method_name, **sample_kwargs_2), None)
        (callback_method, method, called_uri, return_value, result, spaces_context, kwargs) = dummy_queue.get(True)
        self.assertEqual(callback_method, dummy_method_name)
        self.assertEqual(method, 'remove')
        self.assertEqual(called_uri, uri.dummyuri)
        self.assertEqual(result, e) 
        self.assertEqual(return_value, None)
        self.assertEqual(spaces_context, dummy_spaces_context)
        self.assertEqual(kwargs, sample_kwargs_2)
               
    def test_watch_case_1(self):
        '''[Spaces] test_watch add watch'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        sample_kwargs = {'one':1, 'two':2}         
        self.assertNotEqual(spaces.watch(['get', 'delete'], uri, dummy_spaces_context, dummy_method_name, **sample_kwargs), None)
        self.assertEqual(spaces.uri_tree.find_node(uri).watch['get'], (dummy_method_name, dummy_spaces_context, sample_kwargs))
        self.assertEqual(spaces.uri_tree.find_node(uri).watch['delete'], (dummy_method_name, dummy_spaces_context, sample_kwargs))

    def test_watch_case_2(self):
        '''[Spaces] test_watch test duplicate call to watch'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        sample_kwargs = {'one':1, 'two':2}      
        self.assertNotEqual(spaces.watch(['get'], uri, dummy_spaces_context, dummy_method_name, **sample_kwargs), None)
        self.assertEqual(spaces.uri_tree.find_node(uri).watch['get'], (dummy_method_name, dummy_spaces_context, sample_kwargs))
        self.assertRaises(Exception, spaces.watch, ['get'], uri, dummy_spaces_context, dummy_method_name)
        self.assertEqual(spaces.uri_tree.find_node(uri).watch['get'], (dummy_method_name, dummy_spaces_context, sample_kwargs))
        
    def test_watch_case_3(self):
        '''[Spaces] test_watch test adding more than one watch for a method'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name_1 = 'dummy_method_name_1'
        dummy_method_name_2 = 'dummy_method_name_2'      
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        sample_kwargs_1 = {'one':1, 'two':2} 
        sample_kwargs_2 = {'three':3}
        self.assertNotEqual(spaces.watch(['get'], uri, dummy_spaces_context, dummy_method_name_1, **sample_kwargs_1), None)
        self.assertRaises(Exception, spaces.watch, ['get'], uri, dummy_spaces_context, dummy_method_name_2, **sample_kwargs_2)
        self.assertEqual(spaces.uri_tree.find_node(uri).watch['get'], (dummy_method_name_1, dummy_spaces_context, sample_kwargs_1))        
    
    def test_watch_case_4(self):
        '''[Spaces] test_watch test watch on different methods'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        sample_kwargs_1 = {'one':1, 'two':2} 
        sample_kwargs_2 = {'three':3}
        self.assertNotEqual(spaces.watch(['get'], uri, dummy_spaces_context, dummy_method_name, **sample_kwargs_1), None)
        self.assertNotEqual(spaces.watch(['put'], uri, dummy_spaces_context, dummy_method_name, **sample_kwargs_2), None)
        self.assertEqual(spaces.uri_tree.find_node(uri).watch['get'], (dummy_method_name, dummy_spaces_context, sample_kwargs_1))
        self.assertEqual(spaces.uri_tree.find_node(uri).watch['put'], (dummy_method_name, dummy_spaces_context, sample_kwargs_2))
        
    def test_watch_all_case_1(self):
        '''[Spaces] test_watch_all add watch_all'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        sample_kwargs = {'one':1, 'two':2}         
        self.assertNotEqual(spaces.watch_all(['get'], uri, dummy_spaces_context, dummy_method_name, **sample_kwargs), None)
        self.assertEqual(spaces.uri_tree.find_node(uri).watch_all['get'], (dummy_method_name, dummy_spaces_context, sample_kwargs))

    def test_watch_all_case_2(self):
        '''[Spaces] test_watch_all test duplicate call to watch_all'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        sample_kwargs = {'one':1, 'two':2} 
        self.assertNotEqual(spaces.watch_all(['get'], uri, dummy_spaces_context, dummy_method_name, **sample_kwargs), None)
        self.assertEqual(spaces.uri_tree.find_node(uri).watch_all['get'], (dummy_method_name, dummy_spaces_context, sample_kwargs))
        self.assertRaises(Exception, spaces.watch_all, ['get'], uri, dummy_spaces_context, dummy_method_name)
        self.assertEqual(spaces.uri_tree.find_node(uri).watch_all['get'], (dummy_method_name, dummy_spaces_context, sample_kwargs))
        
    def test_watch_all_case_3(self):
        '''[Spaces] test_watch_all test adding more than one watch_all for a method'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name_1 = 'dummy_method_name_1'
        dummy_method_name_2 = 'dummy_method_name_2'     
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        sample_kwargs_1 = {'one':1, 'two':2} 
        sample_kwargs_2 = {'three':3}        
        self.assertNotEqual(spaces.watch_all(['get'], uri, dummy_spaces_context, dummy_method_name_1, **sample_kwargs_1), None)
        self.assertRaises(Exception, spaces.watch_all, ['get'], uri, dummy_spaces_context, dummy_method_name_2, **sample_kwargs_2)
        self.assertEqual(spaces.uri_tree.find_node(uri).watch_all['get'], (dummy_method_name_1, dummy_spaces_context, sample_kwargs_1))        
    
    def test_watch_all_case_4(self):
        '''[Spaces] test_watch_all test watch_all on different methods'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        sample_kwargs_1 = {'one':1, 'two':2} 
        sample_kwargs_2 = {'three':3}         
        self.assertNotEqual(spaces.watch_all(['get'], uri, dummy_spaces_context, dummy_method_name, **sample_kwargs_1), None)
        self.assertNotEqual(spaces.watch_all(['put'], uri, dummy_spaces_context, dummy_method_name, **sample_kwargs_2), None)
        self.assertEqual(spaces.uri_tree.find_node(uri).watch_all['get'], (dummy_method_name, dummy_spaces_context, sample_kwargs_1))
        self.assertEqual(spaces.uri_tree.find_node(uri).watch_all['put'], (dummy_method_name, dummy_spaces_context, sample_kwargs_2))        

    def test_delete_watch(self):
        '''[Spaces] test_delete_watch'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        get_watch_ref_1 = spaces.watch_all(['get', 'delete'], uri, dummy_spaces_context, dummy_method_name)
        put_watch_ref_1 = spaces.watch(['put'], uri, dummy_spaces_context, dummy_method_name)
        self.assertEqual(spaces.uri_tree.find_node(uri).watch_all['get'], (dummy_method_name, dummy_spaces_context, {}))
        self.assertEqual(spaces.uri_tree.find_node(uri).watch_all['delete'], (dummy_method_name, dummy_spaces_context, {}))
        self.assertEqual(spaces.uri_tree.find_node(uri).watch['put'], (dummy_method_name, dummy_spaces_context, {}))
        self.assertEqual(spaces.delete_watch(get_watch_ref_1), None)
        self.assertFalse('get' in spaces.uri_tree.find_node(uri).watch_all)
        self.assertFalse('delete' in spaces.uri_tree.find_node(uri).watch_all)
        self.assertEqual(spaces.uri_tree.find_node(uri).watch['put'], (dummy_method_name, dummy_spaces_context, {}))
        self.assertEqual(spaces.delete_watch(put_watch_ref_1), None)
        self.assertFalse('get' in spaces.uri_tree.find_node(uri).watch_all)
        self.assertFalse('delete' in spaces.uri_tree.find_node(uri).watch_all)        
        self.assertFalse('put' in spaces.uri_tree.find_node(uri).watch)
                
    def test_process_case_1(self):
        '''[Spaces] test_process add process'''
        dummy_base = 'http://dummy.com'
        dummy_method_name_get = 'dummy_method_name_get'
        dummy_method_name_put = 'dummy_method_name_put'
        dummy_method_name_delete = 'dummy_method_name_delete'
        dummy_method_name_find = 'dummy_method_name_find'
        dummy_method_name_remove = 'dummy_method_name_remove'
        callback_dict = {'get':dummy_method_name_get,
                         'put':dummy_method_name_put,
                         'delete':dummy_method_name_delete,
                         'find':dummy_method_name_find,
                         'remove':dummy_method_name_remove}
        uri = Uri(dummy_base).collection(True)
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        sample_kwargs = {'one':1, 'two':2} 
        self.assertNotEqual(spaces.process(uri, callback_dict, dummy_spaces_context, **sample_kwargs), None)
        self.assertEqual(spaces.uri_tree.find_node(uri).process['get'], (dummy_method_name_get, dummy_spaces_context, sample_kwargs))
        self.assertEqual(spaces.uri_tree.find_node(uri).process['put'], (dummy_method_name_put, dummy_spaces_context, sample_kwargs))
        self.assertEqual(spaces.uri_tree.find_node(uri).process['delete'], (dummy_method_name_delete, dummy_spaces_context, sample_kwargs))
        self.assertEqual(spaces.uri_tree.find_node(uri).process['find'], (dummy_method_name_find, dummy_spaces_context, sample_kwargs))
        self.assertEqual(spaces.uri_tree.find_node(uri).process['remove'], (dummy_method_name_remove, dummy_spaces_context, sample_kwargs))
                
    def test_process_case_2(self):
        '''[Spaces] test_process test duplicate call to process'''
        dummy_base = 'http://dummy.com'
        dummy_method_name_get = 'dummy_method_name_get'
        dummy_method_name_put = 'dummy_method_name_put'
        dummy_method_name_delete = 'dummy_method_name_delete'
        dummy_method_name_find = 'dummy_method_name_find'
        dummy_method_name_remove = 'dummy_method_name_remove'
        callback_dict = {'get':dummy_method_name_get,
                         'put':dummy_method_name_put,
                         'delete':dummy_method_name_delete,
                         'find':dummy_method_name_find,
                         'remove':dummy_method_name_remove}
        uri = Uri(dummy_base).collection(True)
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        sample_kwargs_1 = {'one':1, 'two':2} 
        sample_kwargs_2 = {'three':3} 
        self.assertNotEqual(spaces.process(uri, callback_dict, dummy_spaces_context, **sample_kwargs_1), None)
        self.assertRaises(Exception, spaces.process, uri, callback_dict, dummy_spaces_context, **sample_kwargs_2)
        self.assertEqual(spaces.uri_tree.find_node(uri).process['get'], (dummy_method_name_get, dummy_spaces_context, sample_kwargs_1))          

    def test_process_all_case_1(self):
        '''[Spaces] test_process_all add process_all'''
        dummy_base = 'http://dummy.com'
        dummy_method_name_get = 'dummy_method_name_get'
        dummy_method_name_put = 'dummy_method_name_put'
        dummy_method_name_delete = 'dummy_method_name_delete'
        dummy_method_name_find = 'dummy_method_name_find'
        dummy_method_name_remove = 'dummy_method_name_remove'
        callback_dict = {'get':dummy_method_name_get,
                         'put':dummy_method_name_put,
                         'delete':dummy_method_name_delete,
                         'find':dummy_method_name_find,
                         'remove':dummy_method_name_remove}
        uri = Uri(dummy_base).collection(True)
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        sample_kwargs = {'one':1, 'two':2} 
        self.assertNotEqual(spaces.process_all(uri, callback_dict, dummy_spaces_context, **sample_kwargs), None)
        self.assertEqual(spaces.uri_tree.find_node(uri).process_all['get'], (dummy_method_name_get, dummy_spaces_context, sample_kwargs))
        self.assertEqual(spaces.uri_tree.find_node(uri).process_all['put'], (dummy_method_name_put, dummy_spaces_context, sample_kwargs))
        self.assertEqual(spaces.uri_tree.find_node(uri).process_all['delete'], (dummy_method_name_delete, dummy_spaces_context, sample_kwargs))
        self.assertEqual(spaces.uri_tree.find_node(uri).process_all['find'], (dummy_method_name_find, dummy_spaces_context, sample_kwargs))      
        self.assertEqual(spaces.uri_tree.find_node(uri).process_all['remove'], (dummy_method_name_remove, dummy_spaces_context, sample_kwargs))        
                
    def test_process_all_case_2(self):
        '''[Spaces] test_process_all test duplicate call to process_all'''
        dummy_base = 'http://dummy.com'
        dummy_method_name_get = 'dummy_method_name_get'
        dummy_method_name_put = 'dummy_method_name_put'
        dummy_method_name_delete = 'dummy_method_name_delete'
        dummy_method_name_find = 'dummy_method_name_find'
        dummy_method_name_remove = 'dummy_method_name_remove'
        callback_dict = {'get':dummy_method_name_get,
                         'put':dummy_method_name_put,
                         'delete':dummy_method_name_delete,
                         'find':dummy_method_name_find,
                         'remove':dummy_method_name_remove}
        uri = Uri(dummy_base).collection(True)
        spaces = Spaces()
        dummy_queue = Queue()   
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        sample_kwargs_1 = {'one':1, 'two':2} 
        sample_kwargs_2 = {'three':3}         
        self.assertNotEqual(spaces.process_all(uri, callback_dict, dummy_spaces_context, **sample_kwargs_1), None)
        self.assertRaises(Exception, spaces.process_all, uri, callback_dict, dummy_spaces_context, **sample_kwargs_2)            
        self.assertEqual(spaces.uri_tree.find_node(uri).process_all['get'], (dummy_method_name_get, dummy_spaces_context, sample_kwargs_1))      

    def test_delete_process(self):
        '''[Spaces] test_delete_process'''
        dummy_base = 'http://dummy.com'
        dummy_process_method_name_get = 'dummy_process_method_name_get'
        dummy_process_method_name_put = 'dummy_process_method_name_put'
        dummy_process_method_name_delete = 'dummy_process_method_name_delete'
        dummy_process_method_name_find = 'dummy_process_method_name_find'
        dummy_process_method_name_remove = 'dummy_method_name_remove'
        dummy_process_callback_dict = {'get':dummy_process_method_name_get,
                         'put':dummy_process_method_name_put,
                         'delete':dummy_process_method_name_delete,
                         'find':dummy_process_method_name_find,
                         'remove':dummy_process_method_name_remove}
        dummy_process_all_method_name_get = 'dummy_process_all_method_name_get'
        dummy_process_all_method_name_put = 'dummy_process_all_method_name_put'
        dummy_process_all_method_name_delete = 'dummy_process_all_method_name_delete'
        dummy_process_all_method_name_find = 'dummy_process_all_method_name_find'
        dummy_process_all_method_name_remove = 'dummy_method_name_remove'
        dummy_process_all_callback_dict = {'get':dummy_process_all_method_name_get,
                         'put':dummy_process_all_method_name_put,
                         'delete':dummy_process_all_method_name_delete,
                         'find':dummy_process_all_method_name_find,
                         'remove':dummy_process_all_method_name_remove}      
        uri = Uri(dummy_base).collection(True)
        spaces = Spaces()
        dummy_queue = Queue()  
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)        
        process_ref_1 = spaces.process_all(uri, dummy_process_all_callback_dict, dummy_spaces_context)
        process_ref_2 = spaces.process(uri, dummy_process_callback_dict, dummy_spaces_context)
        self.assertEqual(spaces.uri_tree.find_node(uri).process_all['get'], (dummy_process_all_method_name_get, dummy_spaces_context, {}))
        self.assertEqual(spaces.uri_tree.find_node(uri).process['put'], (dummy_process_method_name_put, dummy_spaces_context, {}))
        self.assertEqual(spaces.delete_process(process_ref_1), None)
        self.assertFalse('get' in spaces.uri_tree.find_node(uri).process_all)
        self.assertEqual(spaces.uri_tree.find_node(uri).process['put'], (dummy_process_method_name_put, dummy_spaces_context, {}))
        self.assertEqual(spaces.delete_process(process_ref_2), None)
        self.assertFalse('get' in spaces.uri_tree.find_node(uri).process_all)
        self.assertTrue(len(spaces.uri_tree.find_node(uri).process) == 0)
    
    def test_find_case_1(self):
        '''[Spaces] test_find prefixes'''
        dummy_base_1 = 'http://dummy1.com'
        dummy_base_2 = 'http://dummy2.com'
        uri_1 = Uri(dummy_base_1).collection(True)
        uri_2 = Uri(dummy_base_2).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)    
        spaces.get(uri_1, dummy_spaces_context)
        spaces.get(uri_2, dummy_spaces_context)
        sample_kwargs = {'one':1, 'two':2} 
        uris = spaces.find(dummy_spaces_context, **sample_kwargs)
        self.assertEqual(len(uris), 2)
        self.assertTrue(uri_1 in uris)
        self.assertTrue(uri_2 in uris)
        
    def test_find_case_2(self):
        '''[Spaces] test_find without callback'''
        dummy_base_1 = 'http://dummy1.com'
        uri_1 = Uri(dummy_base_1).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)        
        spaces.get(uri_1.a, dummy_spaces_context)
        uris = spaces.find(dummy_spaces_context, uri_1)
        self.assertEqual(len(uris), 1)
        self.assertTrue(uri_1.a in uris)
        spaces.get(uri_1.b, dummy_spaces_context)
        uris = spaces.find(dummy_spaces_context, uri_1)
        self.assertEqual(len(uris), 2)
        self.assertTrue(uri_1.a in uris)
        self.assertTrue(uri_1.b in uris)  
        spaces.get(uri_1.c.d, dummy_spaces_context)
        uris = spaces.find(dummy_spaces_context, uri_1)
        self.assertEqual(len(uris), 3)
        self.assertTrue(uri_1.a in uris)
        self.assertTrue(uri_1.b in uris)        
        self.assertTrue(uri_1.c.collection(True) in uris)
        uris = spaces.find(dummy_spaces_context, uri_1.c.d)
        self.assertEqual(len(uris), 0)               

    def test_find_case_3(self):
        '''[Spaces] test_find with callback'''
        dummy_base_1 = 'http://dummy1.com'
        dummy_base_2 = 'http://dummy2.com'
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        uri_1 = Uri(dummy_base_1).collection(True)
        uri_2 = Uri(dummy_base_2).collection(True)
        dummy_resource = Resource()
        spaces.get(uri_1, dummy_spaces_context)
        spaces.get(uri_2, dummy_spaces_context)
        sample_kwargs = {'one':1, 'two':2} 
        self.assertEqual(spaces.find(spaces_context=dummy_spaces_context, callback=dummy_method_name, **sample_kwargs), None)
        (callback, method, uri, list_of_uris, return_value, caller_spaces_context, kwargs) = dummy_queue.get()
        self.assertEqual(callback, dummy_method_name)
        self.assertEqual(method, 'find')
        self.assertEqual(uri, None)
        self.assertEqual(len(list_of_uris), 2)
        self.assertTrue(uri_1 in list_of_uris)
        self.assertTrue(uri_2 in list_of_uris)
        self.assertEqual(return_value, None)
        self.assertEqual(caller_spaces_context, dummy_spaces_context)
        self.assertEqual(kwargs, sample_kwargs)

    def test_find_case_4(self):
        '''[Spaces] test_find no callback, with process'''
        dummy_base = 'http://dummy.com/'
        uri = Uri(dummy_base)
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        process_dummy_queue = Queue()
        process_dummy_method_name = 'process_dummy_method_name'        
        process_dummy_spaces_context = SpacesContext(spaces, process_dummy_queue)   
        sample_kwargs_1 = {'one':1, 'two':2} 
        sample_kwargs_2 = {'three':3}          
        node = Node(dummy_base)
        node.process['find'] = (process_dummy_method_name, process_dummy_spaces_context, sample_kwargs_1)  
        spaces.uri_tree.add_node(node, None)          
        def emulate_task_response():
            (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = process_dummy_queue.get(True)
            self.assertEqual(process_callback, process_dummy_method_name)
            self.assertEqual(method, 'find')
            self.assertEqual(called_uri, uri)
            self.assertEqual(result, None)
            self.assertEqual(return_value, None)    
            self.assertEqual(kwargs, sample_kwargs_1)
            spaces_context.return_from_callback(spaces_context, return_value=[uri.a, uri.b])
        thread.start_new_thread(emulate_task_response, ())
        self.assertEqual(spaces.find(dummy_spaces_context, uri, **sample_kwargs_2), [uri.a, uri.b])

    def test_find_case_5(self):
        '''[Spaces] test_find with callback, with process'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()    
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        process_dummy_queue = Queue()
        process_dummy_method_name = 'process_dummy_method_name'        
        process_dummy_spaces_context = SpacesContext(spaces, process_dummy_queue) 
        sample_kwargs_1 = {'one':1, 'two':2} 
        sample_kwargs_2 = {'three':3}             
        node = Node('dummyuri')
        node.process['find'] = (process_dummy_method_name, process_dummy_spaces_context, sample_kwargs_1)       
        spaces.uri_tree.add_node(node, uri)
        def emulate_task_response():
            (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = process_dummy_queue.get(True)
            self.assertEqual(process_callback, process_dummy_method_name)
            self.assertEqual(method, 'find')
            self.assertEqual(called_uri, uri.dummyuri)
            self.assertEqual(result, None)
            self.assertEqual(return_value, None)
            self.assertEqual(kwargs, sample_kwargs_1)
            spaces.return_from_callback(spaces_context, return_value=[uri.dummyuri.sillyuri])
        thread.start_new_thread(emulate_task_response, ())        
        self.assertEqual(spaces.find(dummy_spaces_context, uri.dummyuri, dummy_method_name, **sample_kwargs_2), None)
        (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = dummy_queue.get(True)
        self.assertEqual(return_value, [uri.dummyuri.sillyuri])
        self.assertEqual(process_callback, dummy_method_name)
        self.assertEqual(method, 'find')
        self.assertEqual(called_uri, uri.dummyuri)
        self.assertEqual(result, None)
        self.assertEqual(spaces_context, dummy_spaces_context)
        self.assertEqual(kwargs, sample_kwargs_2)

    def test_find_case_6(self):
        '''[Spaces] test_find no callback, with process with exception'''
        dummy_base = 'http://dummy.com/'
        uri = Uri(dummy_base)
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        process_dummy_queue = Queue()
        process_dummy_method_name = 'process_dummy_method_name'        
        process_dummy_spaces_context = SpacesContext(spaces, process_dummy_queue)   
        sample_kwargs_1 = {'one':1, 'two':2} 
        sample_kwargs_2 = {'three':3}          
        node = Node(dummy_base)
        node.process['find'] = (process_dummy_method_name, process_dummy_spaces_context, sample_kwargs_1)  
        spaces.uri_tree.add_node(node, None)          
        def emulate_task_response():
            (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = process_dummy_queue.get(True)
            self.assertEqual(process_callback, process_dummy_method_name)
            self.assertEqual(method, 'find')
            self.assertEqual(called_uri, uri)
            self.assertEqual(result, None)
            self.assertEqual(return_value, None)
            self.assertEqual(kwargs, sample_kwargs_1)    
            spaces_context.return_from_callback(spaces_context, exception=Exception())
        thread.start_new_thread(emulate_task_response, ())
        self.assertRaises(Exception, spaces.find, dummy_spaces_context, uri, **sample_kwargs_2)

    def test_find_case_7(self):
        '''[Spaces] test_find with callback, with process with exception'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()    
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)
        process_dummy_queue = Queue()
        process_dummy_method_name = 'process_dummy_method_name'        
        process_dummy_spaces_context = SpacesContext(spaces, process_dummy_queue)  
        sample_kwargs_1 = {'one':1, 'two':2} 
        sample_kwargs_2 = {'three':3}           
        e = Exception()    
        node = Node('dummyuri')
        node.process['find'] = (process_dummy_method_name, process_dummy_spaces_context, sample_kwargs_1)       
        spaces.uri_tree.add_node(node, uri)
        def emulate_task_response():
            (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = process_dummy_queue.get(True)
            self.assertEqual(process_callback, process_dummy_method_name)
            self.assertEqual(method, 'find')
            self.assertEqual(called_uri, uri.dummyuri)
            self.assertEqual(result, None)
            self.assertEqual(return_value, None)
            self.assertEqual(kwargs, sample_kwargs_1)
            spaces.return_from_callback(spaces_context, exception=e)
        thread.start_new_thread(emulate_task_response, ())        
        self.assertEqual(spaces.find(dummy_spaces_context, uri.dummyuri, dummy_method_name, **sample_kwargs_2), None)
        (process_callback, method, called_uri, return_value, result, spaces_context, kwargs) = dummy_queue.get(True)
        self.assertEqual(return_value, None)
        self.assertEqual(process_callback, dummy_method_name)
        self.assertEqual(method, 'find')
        self.assertEqual(called_uri, uri.dummyuri)
        self.assertEqual(result, e)
        self.assertEqual(spaces_context, dummy_spaces_context)
        self.assertEqual(kwargs, sample_kwargs_2)

    def test_find_case_8(self):
        '''[Spaces] test_find with no children'''
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)        
        uris = spaces.find(dummy_spaces_context)
        self.assertEqual(len(uris), 0)
         
        dummy_base_1 = 'http://dummy1.com'
        uri_1 = Uri(dummy_base_1).collection(True)
        dummy_resource = Resource()
        spaces = Spaces()
        dummy_queue = Queue()
        dummy_method_name = 'dummy_method_name'        
        dummy_spaces_context = SpacesContext(spaces, dummy_queue)        
        spaces.get(uri_1, dummy_spaces_context)
        uris = spaces.find(dummy_spaces_context, uri_1)
        self.assertEqual(len(uris), 0)                
            
    def test_return_from_callback_case_1(self):
        '''[Spaces] return_from_callback with callback'''
        dummy_callback_spaces_queue = Queue()
        dummy_callback_method_name = 'dummy_callback_method_name'
        method = 'get'
        dummy_base = 'http://dummy.com'
        return_value = 'abc'
        uri = Uri(dummy_base).collection(True)
        node = Node()
        spaces = Spaces()
        sample_kwargs = {'one':1, 'two':2} 
        dummy_spaces_context = SpacesContext(spaces, dummy_callback_spaces_queue)
        spaces.callback_return_info[dummy_spaces_context] = (dummy_spaces_context, dummy_callback_method_name, method, uri, node, sample_kwargs)
        spaces.return_from_callback(dummy_spaces_context, return_value)
        self.assertEqual(dummy_callback_spaces_queue.get(True), (dummy_callback_method_name, method, uri, return_value, None, dummy_spaces_context, sample_kwargs))
        
    def test_return_from_callback_case_2(self):
        '''[Spaces] return_from_callback with no callback'''
        dummy_callback_spaces_queue = Queue()
        dummy_base = 'http://dummy.com'
        return_value = 'abc'
        lock = threading.Lock()
        spaces = Spaces()
        dummy_spaces_context = SpacesContext(spaces, dummy_callback_spaces_queue)
        spaces.callback_return_info[dummy_spaces_context] = lock
        lock.acquire()
        spaces.return_from_callback(dummy_spaces_context, return_value)
        self.assertEqual(spaces.callback_return_info[dummy_spaces_context], (lock, return_value, None))
    
    def test_get_res_part(self):
        '''[Spaces] test get_res_part'''
        # We need a test spaces environment
        ns_dict = {'a': 'http://www.example.com/',
                   'b': 'http://www.dummy.com/'}
        spaces = Spaces()
        spaces_queue = Queue()
        spaces_context = SpacesContext(spaces, spaces_queue) 
        spaces.ns = ns_dict
        
        uri = Uri("a:")
    
        res1 = Resource()
        res1.a = "qaaa"
        res1.b = [['a1', ['qaa1', 'qaa2']], ['b1', ['qbb1', 'qbb2']]]
        res1.x = [{'a1':'q11','a2':'q12'}, {'a1':'q21','a2':'q22'}, {'a1':'q31','a2':'q32'}]
        spaces_context.put(uri.res1, res1, spaces_context)
        
        res2 = Resource()
        res2a = "res2 var a"
        res2.a = res2a
        spaces_context.put(uri.res2, res2, spaces_context)

        self.assertEqual(spaces_context.get_res_part(Uri("a:res1#a")), 'qaaa')
        self.assertEqual(spaces_context.get_res_part(Uri("a:res1#b.1.0")), 'b1')
        self.assertEqual(spaces_context.get_res_part(Uri("a:res1#x.1.a2")), 'q22')
        self.assertEqual(spaces_context.get_res_part(Uri("a:res2#a")), res2a)
        self.assertEqual(spaces_context.get_res_part(Uri("a:res2#")), res2.get_vars())
        self.assertEqual(spaces_context.get_res_part(Uri("a:res2")), res2.get_vars())
        self.assertEqual(spaces_context.get_res_part(uri.fred), None)

    def test_find_all_case_1(self):
        '''[Spaces] find_all'''
        dummy_base = 'http://dummy.com'
        uri = Uri(dummy_base).collection(True)
        dummy_resource = Resource()        
        spaces = Spaces()
        queue = Queue()
        spaces_context = SpacesContext(spaces, queue)        
        spaces_context.put(uri.a, dummy_resource)
        spaces_context.put(uri.b, dummy_resource)
        spaces_context.put(uri.a.b, dummy_resource)
        spaces_context.put(uri.a.z.z, dummy_resource)
        #for u in spaces_context.find_all():
        #    print(u)
        find_all = spaces_context.find_all()
        #self.assertEqual(find_all[0], uri, 'dump not equal %s - %s' % (find_all[0], uri))  
        self.assertEqual(find_all[0], uri.a, 'dump not equal %s - %s' % (find_all[0], uri.a))  
        self.assertEqual(find_all[1], uri.b, 'dump not equal %s - %s' % (find_all[1], uri.b))  
        self.assertEqual(find_all[2], uri.a.b, 'dump not equal %s - %s' % (find_all[2], uri.a.b))  
        self.assertEqual(find_all[3], uri.a.z.z, 'dump not equal %s - %s' % (find_all[3], uri.a.z.z))          
    
    def test_issue_process_callback(self):
        '''[spaces] test issue_process_callback where callback defined for a particular method of a uri that is being processed by a task'''
        spaces = Spaces()
        process_callback = None
        process_spaces_queue = Queue()
        process_spaces_context = SpacesContext(spaces, process_spaces_queue)
        method = 'get'
        blah_uri = Uri('blah')
        node = Node()
        res = None
        sample_kwargs_1 = {'one':1, 'two':2} 
        sample_kwargs_2 = {'three':3}   
                
        # test first scenario where caller does not define callback        
        caller_callback = None
        caller_spaces_context = None
        process_callback_info = (process_callback, process_spaces_context, sample_kwargs_1)
        return_value = spaces.issue_process_callback(process_callback_info, caller_callback, caller_spaces_context, method, blah_uri, node, res, **sample_kwargs_2)
        self.assertEqual(return_value, (None, None))

        # test second scenario where caller does define a callback
        caller_callback = 'dummy_callback'
        caller_spaces_queue = Queue()
        caller_spaces_context = SpacesContext(spaces, caller_spaces_queue)
        process_callback_info = (process_callback, process_spaces_context, sample_kwargs_1)
        return_value = spaces.issue_process_callback(process_callback_info, caller_callback, caller_spaces_context, method, blah_uri, node, res, **sample_kwargs_2)
        self.assertEqual(return_value, (None, None))
        (callback, method, uri, return_value, result, spaces_context, kwargs) = caller_spaces_queue.get(True)
        self.assertEqual(callback, caller_callback)
        self.assertEqual(method, 'get')
        self.assertEqual(uri, blah_uri)
        self.assertEqual(return_value, None)
        self.assertEqual(result, None)
        self.assertEqual(spaces_context, caller_spaces_context)
        self.assertEqual(kwargs, sample_kwargs_2)
        
class SpacesNodeTest(unittest.TestCase):
        
    def test_add_child(self):
        '''[NodeTest] add child '''
        node1 = Node('http://www.example.com')
        node2 = Node('a')
        self.assertEqual(node1.add_child_node(node2), node2)
        self.assertTrue(node2 in node1.children.values())
        self.assertEqual(node2.parent, node1)

    def test_find_child(self):
        '''[NodeTest] find child '''
        node1 = Node('http://www.example.com')
        node2 = Node('a')
        node3 = Node('b')
        node4 = Node('c')
        self.assertEqual(node1.add_child_node(node2), node2)
        self.assertEqual(node1.add_child_node(node3), node3)
        self.assertEqual(node1.find_node('a'), node2)
        self.assertEqual(node1.find_node('b'), node3)
        self.assertNotEqual(node1.find_node('c'), node4)
        
    def test_delete_child_case_1(self):
        '''[NodeTest] delete child'''
        node1 = Node('http://www.example.com')
        node2 = Node('a')
        node3 = Node('b')
        self.assertEqual(node1.add_child_node(node2), node2)
        self.assertEqual(node1.add_child_node(node3), node3)
        self.assertEqual(node1.find_node('a'), node2)
        self.assertEqual(node1.find_node('b'), node3)
        self.assertEqual(node1.delete_node(node2), node2)
        self.assertNotEqual(node1.find_node('a'), node2)
        self.assertEqual(node1.find_node('b'), node3)
        
    def test_delete_child_case_2(self):
        '''[NodeTest] delete_child that doesnt exist'''
        node1 = Node('http://www.example.com')
        node2 = Node('a')
        self.assertEqual(node1.delete_node(node2), None)                

    def test_property_accessors(self):
        '''[NodeTest] property accessors'''
        node1 = Node('http://www.example.com')
        node2 = Node('a')
        node3 = Node('b')
        node1.add_child_node(node2)
        node2.add_child_node(node3)
        self.assertEqual(node2.parent, node1)
        self.assertTrue(node3 in node2.children.values())
        self.assertEqual(node2.part, 'a')
        self.assertFalse(node2.watch)
        self.assertFalse(node2.watch_all)
        self.assertFalse(node2.process)
        self.assertFalse(node2.process_all)
        self.assertFalse(node2.resource)
        
    def test_uri(self):
        '''[NodeTest] test uri'''
        node1 = Node('http://www.example.com/')
        node2 = Node('a/')
        node3 = Node('b')
        node1.add_child_node(node2)
        node2.add_child_node(node3)
        self.assertEqual(node3.uri(), 'http://www.example.com/a/b')
        self.assertEqual(node2.uri(), 'http://www.example.com/a/')
        self.assertEqual(node1.uri(), 'http://www.example.com/')
    
    def test_get_resource_wait_case_1(self):
        '''[Node] test get_resource when resource is already there'''
        node1 = Node('http://www.example.com')
        rs = Resource()
        node1.resource = rs
        self.assertEqual(node1.get_resource_wait(), rs)
        
    def test_get_resource_wait_case_2(self):
        '''[Node] test get_resource when resource is added afterwards'''
        node1 = Node('http://www.example.com')
        rs = Resource()
        def delay_add_resource_to_node():
            time.sleep(2)
            node1.resource = rs
        thread.start_new_thread(delay_add_resource_to_node, ())
        self.assertEqual(node1.get_resource_wait(), rs)        
                 
class UriTreeTest(unittest.TestCase):

    def test_find_node_case_1(self):
        '''[UriTree] test find_node where nodes already created'''
        node1 = Node('http://www.example.com/')
        node2 = Node('a/')
        node3 = Node('b/')
        node4 = Node('c/')
        node5 = Node('d/')
        node6 = Node('x/')
        node7 = Node('y/')
        node8 = Node('z/')
        node9 = Node('http://www.blah.com/')
        node10 = Node('e/')
        node1.add_child_node(node2)
        node1.add_child_node(node6)
        node2.add_child_node(node3)
        node2.add_child_node(node4)
        node3.add_child_node(node5)
        node6.add_child_node(node7)
        node7.add_child_node(node8)
        node9.add_child_node(node10)
        uri_1 = Uri('http://www.example.com/')
        uri_2 = Uri('http://www.blah.com/')
        tree = UriTree()
        tree.add_node(node1, None)
        tree.add_node(node9, None)
        self.assertEqual(tree.find_node(uri_1), node1)
        self.assertEqual(tree.find_node(uri_1.a.collection(True)), node2)
        self.assertEqual(tree.find_node(uri_1.a.b.collection(True)), node3)
        self.assertEqual(tree.find_node(uri_1.a.c.collection(True)), node4)
        self.assertEqual(tree.find_node(uri_1.a.b.d.collection(True)), node5)
        self.assertEqual(tree.find_node(uri_1.x.collection(True)), node6)
        self.assertEqual(tree.find_node(uri_1.x.y.collection(True)), node7)
        self.assertEqual(tree.find_node(uri_1.x.y.z.collection(True)), node8)
        self.assertNotEqual(tree.find_node(uri_1.b.collection(True)), None)
        self.assertNotEqual(tree.find_node(uri_1.y.z.collection(True)), None)
        self.assertEqual(tree.find_node(uri_2.collection(True)), node9)
        self.assertEqual(tree.find_node(uri_2.e.collection(True)), node10)
                    
    def test_find_node_case_2(self):
        '''[UriTree] test find_node where node not created'''
        tree = UriTree()
        uri_1 = Uri('/a')
        node = tree.find_node(uri_1)
        self.assertEqual(str(node.uri().full_uri()), uri_1.full_uri())
        uri_1 = Uri('/')
        node = tree.find_node(uri_1)
        self.assertEqual(str(node.uri().full_uri()), uri_1.full_uri())
        uri_1 = Uri('')
        node = tree.find_node(uri_1)
        self.assertEqual(str(node.uri().full_uri()), uri_1.full_uri())
        uri_1 = Uri('/a/')
        node = tree.find_node(uri_1)
        self.assertEqual(str(node.uri().full_uri()), uri_1.full_uri())
        uri_1 = Uri('/a/b')
        node = tree.find_node(uri_1)
        self.assertEqual(str(node.uri().full_uri()), uri_1.full_uri())
        uri_1 = Uri('a/b')
        node = tree.find_node(uri_1)
        self.assertEqual(str(node.uri().full_uri()), uri_1.full_uri())
        uri_1 = Uri('/a/b/')
        node = tree.find_node(uri_1)
        self.assertEqual(str(node.uri().full_uri()), uri_1.full_uri())
        uri_1 = Uri('a/b/')
        node = tree.find_node(uri_1)
        self.assertEqual(str(node.uri().full_uri()), uri_1.full_uri())        
   
    def test_add_child_case_1(self):
        '''[UriTree] test add_child 1'''
        node1 = Node('http://www.example.com/')
        node2 = Node('a/')
        node3 = Node('b/')
        node4 = Node('c/')
        node5 = Node('d/')
        node6 = Node('x/')
        node7 = Node('y/')
        node8 = Node('z/') 
        node9 = Node('http://www.blah.com/')
        node10 = Node('e/')
        uri_1 = Uri('http://www.example.com/')
        uri_2 = Uri('http://www.blah.com/')
        tree = UriTree()
        self.assertEqual(tree.add_node(node1, None), node1)
        self.assertEqual(tree.find_node(uri_1), node1)
        self.assertEqual(tree.add_node(node9, None), node9)
        self.assertEqual(tree.find_node(uri_2), node9)
        self.assertEqual(tree.add_node(node2, uri_1), node2)
        self.assertEqual(tree.find_node(uri_1.a.collection(True)), node2)
        self.assertEqual(tree.add_node(node3, uri_1.a), node3)
        self.assertEqual(tree.find_node(uri_1.a.b.collection(True)), node3)
        self.assertEqual(tree.add_node(node4, uri_1.a), node4)
        self.assertEqual(tree.find_node(uri_1.a.c.collection(True)), node4)
        self.assertEqual(tree.add_node(node5, uri_1.a.b), node5)
        self.assertEqual(tree.find_node(uri_1.a.b.d.collection(True)), node5)
        self.assertEqual(tree.add_node(node6, uri_1), node6)
        self.assertEqual(tree.find_node(uri_1.x.collection(True)), node6)
        self.assertEqual(tree.add_node(node7, uri_1.x), node7)
        self.assertEqual(tree.find_node(uri_1.x.y.collection(True)), node7)
        self.assertEqual(tree.add_node(node8, uri_1.x.y), node8)
        self.assertEqual(tree.find_node(uri_1.x.y.z.collection(True)), node8)
        self.assertEqual(tree.add_node(node10, uri_2), node10)
        self.assertEqual(tree.find_node(uri_2.e.collection(True)), node10)
    
    def test_add_child_case_2(self):
        '''[UriTree] test add_child 2'''
        uri_1 = Uri('http://www.example.com/')
        node = Node('e')
        tree = UriTree()
        self.assertEqual(tree.add_node(node, uri_1.a.b.c.d.collection(True)), node)
        self.assertEqual(tree.find_node(uri_1.a.b.c.d.e), node)
        
    def test_delete_node(self):
        '''[UriTree] test delete node'''
        node1 = Node('http://www.example.com/')
        node2 = Node('a/')
        node3 = Node('b/')
        node4 = Node('c/')
        node5 = Node('d/')
        node6 = Node('x/')
        node7 = Node('y/')
        node8 = Node('z/') 
        node9 = Node('http://www.blah.com/')
        node10 = Node('e/')
        uri_1 = Uri('http://www.example.com/')
        uri_2 = Uri('http://www.blah.com/')
        tree = UriTree()
        self.assertEqual(tree.add_node(node1, None), node1)
        self.assertEqual(tree.add_node(node9, None), node9)
        self.assertEqual(tree.add_node(node2, uri_1), node2)
        self.assertEqual(tree.add_node(node3, uri_1.a.collection(True)), node3)
        self.assertEqual(tree.add_node(node4, uri_1.a.collection(True)), node4)
        self.assertEqual(tree.add_node(node5, uri_1.a.b.collection(True)), node5)
        self.assertEqual(tree.add_node(node6, uri_1), node6)
        self.assertEqual(tree.add_node(node7, uri_1.x.collection(True)), node7)
        self.assertEqual(tree.add_node(node8, uri_1.x.y.collection(True)), node8)
        self.assertEqual(tree.add_node(node10, uri_2), node10)
        self.assertEqual(tree.delete_node(uri_1.x.y.z.collection(True)), node8)
        self.assertNotEqual(tree.find_node(uri_1.x.y.z.collection(True)), node8)
        self.assertEqual(tree.delete_node(uri_1.x.y.collection(True)), node7)
        self.assertNotEqual(tree.find_node(uri_1.x.y.collection(True)), node7)
        self.assertEqual(tree.delete_node(uri_1.x.collection(True)), node6)
        self.assertNotEqual(tree.find_node(uri_1.x.collection(True)), node6)
        self.assertEqual(tree.delete_node(uri_1.a.c.collection(True)), node4)
        self.assertNotEqual(tree.find_node(uri_1.a.c.collection(True)), node4)
        self.assertEqual(tree.delete_node(uri_1.a.collection(True)), node2)
        self.assertNotEqual(tree.find_node(uri_1.a.collection(True)), node2)
        self.assertNotEqual(tree.find_node(uri_1.a.b.collection(True)), node3)
        self.assertNotEqual(tree.find_node(uri_1.a.b.d.collection(True)), node5)
        self.assertEqual(tree.delete_node(uri_2.collection(True)), node9)
        self.assertNotEqual(tree.find_node(uri_2.collection(True)), node9)
    
    def test_delete_node_case_2(self):
        '''[UriTree] delete_node doesnt exist'''
        tree = UriTree()
        self.assertEqual(tree.delete_node(None), None)

                
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()